package com.cody.controller.teacher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cody.entity.*;
import com.cody.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private PassageService passageService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private CommentService commentService;

    @GetMapping({"/index"})
    public String findAll(HttpSession session) {
        List<Question> questions;
        List<Passage> passages;
        questions = questionService.queryAllQuestion();
        passages = passageService.queryAllPassage();
        List<Question> questionsByCommentSum;
        questionsByCommentSum = questionService.queryQuestionByCommentSum();
        List<String> subject;
        List<String> section;
        Teacher teacher = (Teacher) session.getAttribute("user");
        String teacherId = teacher.getTeacherNumber();
        List<Question> questionsIsNotAnswer = new ArrayList<>();
        session.setAttribute("FLAG", questionsIsNotAnswer.size());
        session.setAttribute("questionsIsNotAnswer", questionsIsNotAnswer);
        List<Question> questionsIsAnwser = new ArrayList<>();
        session.setAttribute("questionsIsAnwser", questionsIsAnwser);
        //里面是教师的回答和评论
        for (Question list : questionService.queryQuestionsByTeacherId(teacherId)) {
            //教师未回答的问题
            if (list.getQuestionIsAnswer() == 0) {
                questionsIsNotAnswer.add(list);
            } else {
                questionsIsAnwser.add(list);
            }
        }
        subject = questionService.queryQuestionByQuestionSubject();
        section = questionService.queryQuestionByQuestionSection();
        session.setAttribute("questionsByCommentSum", questionsByCommentSum);
        session.setAttribute("questions", questions);
        session.setAttribute("passages", passages);
        session.setAttribute("subject", subject);
        session.setAttribute("section", section);
        return "/teacher/index";
    }

    @RequestMapping("/article")
    String article() {
        return "/teacher/article";
    }

    @RequestMapping("/outArticle")
    String outArticle() {
        return "/teacher/article_pub";
    }

    @RequestMapping("/articlePub")
    public String articlePub(@RequestParam("title") String title, @RequestParam("content") String content,
                             HttpSession session) {
        Teacher teacher;
        teacher = (Teacher) session.getAttribute("user");
//查询出教师的名字
          teacher = teacherService.querySingleTeacherByTeacherNumber(teacher.getTeacherNumber());
        Passage passage = new Passage(
                4, title, teacher.getTeacherName(), new Date(), content);
        passageService.insertPassage(passage);
        return "redirect:/teacher/index";
    }

    @RequestMapping("/articleDetails/{passageId}")
    String allArticle(@PathVariable("passageId") String passageId, HttpSession session) {
        int pId = Integer.valueOf(passageId);
        Passage passage;
        passage = passageService.querySinglePassage(pId);
        session.setAttribute("passage", passage);
        return "/teacher/articleDetails";
    }


    @GetMapping("/question")
    public String question(HttpSession session) {

        Teacher teacher = (Teacher) session.getAttribute("user");
        String teacherId = teacher.getTeacherNumber();
        List<Comment> comments;
        List<Answer> answers;
        comments = commentService.queryAllCommentByNumber(teacherId);
        answers = answerService.queryAllAnswerByAnswerRespondId(teacherId);
        //当教师回答之后更新isanswer的状态
        session.setAttribute("comments", comments);
        session.setAttribute("answers1", answers);

        return "/teacher/home";
    }

    @GetMapping("/home")
    public String home() {

        return "/teacher/home";
    }

    @RequestMapping("/search")
    public String search(@RequestParam("keyContent") String keyContent, HttpSession session) {
        //关键字根据问题题目进行搜索
        List<Question> questionList;
        questionList = questionService.queryQuestionByQuestionText("%" + keyContent + "%");
        //没有搜索到问题
        String res = "共搜索到" + questionList.size() + "条数据";
        int sum;
        sum = questionList.size();
        session.setAttribute("keyContent", keyContent);
        session.setAttribute("searchres", res);
        session.setAttribute("sum", sum);
        session.setAttribute("questionList", questionList);
        return "/teacher/searchres";

    }

    @RequestMapping("/exitInfo")
    public String exitInfo() {
        return "/teacher/exit";
    }

    // upPwd
    @RequestMapping("upPwd")
    public String upPwd(@RequestParam(required = false, name = "password") String password,
                        @RequestParam(required = false, name = "rePassword") String rePassword, HttpSession session) {
        if (!password.equals(rePassword)) {
            session.setAttribute("msg", "密码输入不一致");
            return "/teacher/set";
        } else {
            Teacher teacher = (Teacher) session.getAttribute("user");
            teacherService.updateTeacherPwd(teacher.getTeacherNumber(), rePassword);
            session.removeAttribute("user");
            return "redirect:/login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //清空session
        session.removeAttribute("user");
        return "redirect:/login";
    }

    @RequestMapping("questionOne/{questionId}")
    public String question(@PathVariable("questionId") Integer questionId, HttpSession session) {
        Question question;
        question = questionService.queryQuestionByQuestionId(questionId);
        List<Answer> answerList;
        answerList = answerService.queryAllAnswerByQuestionId(questionId);
        List<Comment> commentList;
        commentList = commentService.queryAllCommentByQuestionId(questionId);
        session.setAttribute("answerList", answerList);
        session.setAttribute("question", question);
        session.setAttribute("commentList", commentList);
        return "/teacher/questionDetails";
    }


    @RequestMapping("/set")
    public String set(HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("user");
        Teacher teacher1;
        teacher1 = teacherService.querySingleTeacher(teacher.getTeacherNumber(), teacher.getTeacherPassword());
        session.setAttribute("teacher", teacher1);
        return "/teacher/set";
    }

    @RequestMapping("/update")
    public String update(@RequestParam(required = false, name = "username") String username,
                         @RequestParam(required = false, name = "userNumber") String userNumber,
                         @RequestParam(required = false, name = "email") String email,
                         @RequestParam(required = false, name = "teacherBrief") String teacherBrief,
                         @RequestParam(required = false, name = "teacherPost") String teacherPost,
                         @RequestParam(required = false, name = "teacherSubject") String teacherSubject,
                         @RequestParam(required = false, name = "teacherDegree") String teacherDegree) {

        Teacher teacher = new Teacher(1, teacherBrief, userNumber, username, 1, "1", teacherPost, teacherSubject, teacherDegree, email);
        teacherService.updateTeacherInfo(teacher);
        return "redirect:/teacher/set";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, String> del(@RequestParam(required = false, name = "teacherId") int teacherId) {
        Map<String, String> map = new HashMap<>();
        boolean flag =teacherService.delTeacherByTeacherId(teacherId);
        if(flag){
            map.put("msg","删除成功");
        }

     /*   JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(JSON.toJSONString(map));*/
        return map;
    }

    @RequestMapping("/test")
    String test() {
        return "/teacher/test";
    }

    @RequestMapping("/questionSearch")
    public String questionSearch(
            @RequestParam(required = false, name = "questionSubject") String questionSubject,
            @RequestParam(required = false, name = "questionSection") String questionSection,
            HttpSession session) {
        List<Question> AllQuestionLists;
        AllQuestionLists = questionService.queryQuestionByQuestionsubjectAndQuestionsection(questionSubject, questionSection);
        session.setAttribute("listSum", AllQuestionLists.size());
        session.setAttribute("AllQuestionLists", AllQuestionLists);
        return "/teacher/QuestionBySs";
    }

}
